<?php
class DBDP {
	public $db_conn;
	
	function __construct() {
		
		$database_username = "root";
		$database_psaaword = "";
		$database_name = "trunk";
		$this->db_conn = mysql_connect ( "localhost", $database_username, $database_psaaword ) or die ( "Could not connect:" . mysql_error () );
		mysql_select_db ( $database_name, $this->db_conn ) or die ( "can not use database:" . mysql_error () );
		mysql_query ( "set names gbk" );
	}
	function __destruct() {
		
		if ($this->db_conn != "") {
			mysql_close ( $this->db_conn ) or die ( "ERROR:" . mysql_error () );
		}
	}
	
	function saveUserInfo($uid,$age,$trunkage,$sex,$grade,$work,$bear,$area,$money){
		$sql =	"UPDATE user SET " .
				"age = $age ," .
				"trunkage = $trunkage ," .
				"sex = $sex ," .
				"grade = '$grade' ," .
				"work = '$work' ," .
				"bear = $bear ," .
				"area = '$area' ," .
				"money = $money " .
		 		"WHERE id='$uid'";
		
		$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
	}
	
	function getUser($id) {
		$sql = "SELECT * FROM user WHERE id=$id";
		$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
		$row = mysql_fetch_array ( $result );
		return $row;
	}
	
	//-----------------------------------------------------------------
	
	function getStock($uid){
		$sql = "SELECT * FROM userstock u,stock s WHERE u.uid = $uid AND u.code = s.code;";
		$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
		return $result;
	}
	
	function addStock($name,$uid){
		$sql = "SELECT * FROM stock WHERE name='$name' OR code='$name';";
		$data = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
		
		if ( $row = mysql_fetch_array ( $data ) ) {
			$sid = $row['code'];
			$sql = "INSERT INTO userstock (`uid`,`code`) VALUES ($uid,'$sid');";
			$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
		}
	}
	
	function delStock($sid,$uid){
		$sql = "DELETE FROM userstock WHERE uid=$uid AND code='$sid';";
		$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
	}
	
	//-----------------------------------------------------------------
	
	function getRStock($uid){
		$sql = "SELECT * FROM userrecstock u,stock s WHERE u.uid = $uid AND u.code = s.code;";
		$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
		return $result;
	}
	
	function addRStock($name,$uid){
		$sql = "SELECT * FROM stock WHERE name='$name' OR code='$name';";
		$data = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
		
		if ( $row = mysql_fetch_array ( $data ) ) {
			$sid = $row['code'];
			$sql = "INSERT INTO userrecstock (`uid`,`code`) VALUES ($uid,'$sid');";
			$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
		}
	}
	
	function delRStock($sid,$uid){
		$sql = "DELETE FROM userrecstock WHERE uid=$uid AND code='$sid';";
		$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
	}
	
	//-----------------------------------------------------------------
	
	function getStockMore(){
		$sql =	"Select		* " .
				"From		stock s, " .
				"			( " .
				"			Select		code,count(*) num " .
				"			From		userstock " .
				"			Group by	code " .
				"			) c " .
				"Where		s.code = c.code " .
				"Order by	num desc " .
				"limit		10";
		
		$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
		return $result;
	}
	
	function getRStockMore(){
		$sql =	"Select		* " .
				"From		stock s, " .
				"			( " .
				"			Select		code,count(*) num " .
				"			From		userrecstock " .
				"			Group by	code " .
				"			) c " .
				"Where		s.code = c.code " .
				"Order by	num desc " .
				"limit		10";
		
		$result = mysql_query ( $sql, $this->db_conn ) or die ( "ERROR1:" . mysql_error () );
		return $result;
	}
}